package com.haozi.ehub.admin.controller;

import com.haozi.ehub.core.service.TestInfoService;
import com.haozi.ehub.core.util.ReturnDataUtils;
import com.haozi.ehub.entity.entities.ReturnData;
import com.haozi.ehub.entity.entities.TestInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：terry
 * @date ：Created in 2020/6/11 17:05
 * @description：TODO
 * @version: 1.0
 */

@RestController
@RequestMapping("/admin")
@SuppressWarnings(value = "all")
public class TestInfoController {

    @Autowired
    private TestInfoService testInfoService;

    @PreAuthorize(value = "hasAnyRole('admin')")
    @RequestMapping(value = "/test-info/id",method = RequestMethod.GET)
    public ResponseEntity<ReturnData<TestInfo>> testInfo(){
        TestInfo testInfo = testInfoService.getById("11111");
        return ReturnDataUtils.responseEntitySuccess(testInfo);
    }

    @PreAuthorize(value = "isAuthenticated() and hasAnyRole('admin')")
    @RequestMapping(value = "/test-info/from-cache",method = RequestMethod.GET)
    public ResponseEntity<TestInfo> fromeCache(){
        TestInfo testInfo = testInfoService.getByIdFromCache("4");
        return ReturnDataUtils.responseEntitySuccess2(testInfo);
    }

    @PreAuthorize(value = "hasAnyRole('sup_admin')")
    @RequestMapping(value = "/test-info/all",method = RequestMethod.GET)
    public ResponseEntity<List<TestInfo>> getAll(){
        List<TestInfo> testInfoList = testInfoService.getAll();
        TestInfo testInfo = testInfoService.getByIdFromCache("11111");
        TestInfo cacheById = testInfoService.getCacheById("33333");
        if(testInfo != null){
            testInfoList.add(testInfo);
        }
        if (cacheById == null) {
            testInfoList.add(cacheById);
        }
        return ReturnDataUtils.responseEntitySuccess(testInfoList,HttpStatus.OK);
    }
}
